import weakref


class ExpensiveObject:

    def __del__(self):
        print('(Deleting {}'.format(self))


def on_finalize(*args):
    print('on_finalize({!r})'.format(args))


obj = ExpensiveObject()
a = weakref.finalize(obj, on_finalize, 'extra argument')
print(type(a), a)

del obj
print(a)
